<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">

                <el-form-item label="${column.comments}" prop="pid">
            <el-input v-model="dataForm.pid" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="dictType">
            <el-input v-model="dataForm.dictType" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="dictName">
            <el-input v-model="dataForm.dictName" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="dictValue">
            <el-input v-model="dataForm.dictValue" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="sort">
            <el-input v-model="dataForm.sort" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="updater">
            <el-input v-model="dataForm.updater" placeholder="${column.comments}"></el-input>
          </el-form-item>
          <el-form-item label="${column.comments}" prop="updateDate">
            <el-input v-model="dataForm.updateDate" placeholder="${column.comments}"></el-input>
          </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getCtById,putCt,postCt} from '@/network/sys/Ct'
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "ctAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
		          id: '',
          pid: '',
          dictType: '',
          dictName: '',
          dictValue: '',
          remark: '',
          sort: '',
          creator: '',
          createDate: '',
          updater: '',
          updateDate: ''
    });


    const dataRule = reactive({
                pid: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          dictType: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          dictName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          dictValue: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          remark: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          sort: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updater: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updateDate: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]    })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
		getctInfo();
		}

      })
    }

	const getctInfo =()=>{
		getCtById(dataForm.value.id).then(function (res) {
		  if (res.code !== 0) {
		    return ElMessage.error(res.msg);
		  }
		  dataForm.value = _.assignIn({}, dataForm.value, res.data);
		}).catch((err) =>{
		});
	}

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }

        let qing = ()=> {
          return !dataForm.value.id ?  postCt(dataForm.value) : putCt(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef

    }
  },


}
</script>

<style scoped>

</style>
